import re


class MetaCalculator:
    """工厂模式 "元计算器" 用于对输入的算式进行验证和分割"""

    def __init__(self, string: str):
        self.string = string.strip()
        self.text = None

        # 正则规则判断
        flag = re.match('\d+[+|-|*|/]\d+', self.string)
        # 输入错误显示重新输入
        if not flag:
            pass
        else:
            self.text = self.string

    def run(self):
        text = self.text

        nums = re.findall('\d+', text)
        # 拿到
        num1, num2 = float(nums[0]), float(nums[1])
        solution = re.findall('[+|-|*|/]', text)[0]
        solution_map = {
            '+': self.add,
            '-': self.sub,
            '*': self.multiply,
            '/': self.div,
        }

        res = solution_map[solution](num1, num2)
        return res

    def add(self, *args):
        return args[0] + args[1]

    def sub(self, *args):
        return args[0] - args[1]

    def multiply(self, *args):
        return args[0] * args[1]

    def div(self, *args):
        return args[0] / args[1]


def execute():
    while True:
        # 拿到用户输入对象
        text = input(">>>")
        content = MetaCalculator(text)

        if text.upper() == "Q":
            break
        if not content.text:
            print("输入有误(按q可以退出)")
        else:
            res = content.run()
            print(res)


